package cn.wd.user.service.impl;


import cn.wd.common.util.StrUtils;
import cn.wd.model.LoginInfo;
import cn.wd.user.domain.entity.BulletMsg;
import cn.wd.user.domain.entity.ReportTable;
import cn.wd.user.domain.entity.User;
import cn.wd.user.domain.mapper.BulletMsgMapper;
import cn.wd.user.domain.mapper.ReportTableMapper;
import cn.wd.user.domain.mapper.UserMapper;
import cn.wd.user.qo.BulletMsgQueryObject;
import cn.wd.user.service.IBulletMsgService;
import cn.wd.user.utils.TokenManager;
import cn.wd.user.vo.ReportBulletMsg;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class BulletMsgServiceImpl extends ServiceImpl<BulletMsgMapper, BulletMsg> implements IBulletMsgService {

    private final BulletMsgMapper mapper;

    private final UserMapper userMapper;

    private final ReportTableMapper reportTableMapper;

    public BulletMsgServiceImpl(BulletMsgMapper mapper, ReportTableMapper reportTableMapper, UserMapper userMapper) {
        this.mapper = mapper;
        this.reportTableMapper = reportTableMapper;
        this.userMapper = userMapper;
    }
    //
    // @Override
    // public void save(BulletMsg obj) {
    //     mapper.insert(obj);
    // }
    //
    // @Override
    // public void deleteById(Long id) {
    //     mapper.deleteByPrimaryKey(id);
    // }
    //
    // @Override
    // public void updateById(BulletMsg obj) {
    //     mapper.updateByPrimaryKey(obj);
    // }
    //
    // @Override
    // public BulletMsg selectById(Long id) {
    //     return mapper.selectByPrimaryKey(id);
    // }
    //
    // @Override
    // public List<BulletMsg> selectAll() {
    //     return mapper.selectAll();
    // }

    @Override
    public Page<BulletMsg> ListBulletMsgPage(BulletMsgQueryObject qo) {
        LambdaQueryWrapper<BulletMsg> wrapper = new LambdaQueryWrapper<BulletMsg>()
                .ge(qo.getMinLike() != null, BulletMsg::getLikes, qo.getMinLike())
                .le(qo.getMaxLike() != null, BulletMsg::getLikes, qo.getMaxLike())
                .ge(StrUtils.hasText(qo.getBeginDate()), BulletMsg::getReleaseTime, qo.getBeginDate())
                .le(StrUtils.hasText(qo.getEndDate()), BulletMsg::getReleaseTime, qo.getEndDate())
                .like(StrUtils.hasText(qo.getKeyword()), BulletMsg::getContent, qo.getKeyword());
        return baseMapper.selectCustomPage(qo, wrapper);
    }

    @Override
    public void reportBulletMsg(ReportBulletMsg reportBulletMsg) {
        LoginInfo info = TokenManager.get(reportBulletMsg.getToken());
        if (info != null) {
            ReportTable reportTable = new ReportTable();
            User userInfo = userMapper.selectByPrimaryKey(info.getId());
            User user = new User();
            user.setId(userInfo.getId());
            user.setNickname(userInfo.getNickname());

            // TODO 弹幕相关的信息

            BulletMsg bulletMsgInfo = mapper.selectByPrimaryKey(reportBulletMsg.getCid());
            BulletMsg bulletMsg = new BulletMsg();
            bulletMsg.setId(bulletMsgInfo.getId());
            bulletMsg.setContent(bulletMsgInfo.getContent());

            // TODO 执行举报弹幕添加功能
            reportTable.setUser(user);
            reportTable.setBullet(bulletMsg);
            reportTable.setReportContent(reportBulletMsg.getReportInfo());
            reportTable.setReportDesc(reportBulletMsg.getReportDes());
            reportTableMapper.insert(reportTable);
        }
    }
}
